//
//  File.swift
//  App_Login
//
//  Created by 蒋晨成 on 2022/1/4.
//

import Adaptor
import RxCocoa
import RxSwift
import UICore
import UIKit

extension VerifyViewController {
    func bind() {
        let input = VerifyViewModel.Input(
            code: codeTextField.rx.text.orEmpty.asDriver(),
            sendSMSTap: sendSMS.asObservable()
        )

        let output = viewModel.transform(input: input)

        output.mobile.subscribe(onNext: { [weak self] _ in
            self?.mobileLabel.text = "\(LoginModular.localizedString(key: "Verify.Label.Mobile"))\(RegisterCenter.mobile ?? "")"
        })
        .disposed(by: disposeBag)
        output.code.subscribe(onNext: { [weak self] code in
            guard let code = code else { return }
            self?.setupCodeUI(code: code)
        })
        .disposed(by: disposeBag)

        output.timer.subscribe(onNext: { [weak self] second in
            self?.setupTimerButton(remain: 59 - second)
        })
        .disposed(by: disposeBag)

        output.submitState.drive(onNext: { [weak self] state in
            guard let self = self else { return }
            switch state {
            case .doing:
                self.view.makeToastActivity(.center)
            case .success:
                self.view.hideToastActivity()
                enterAppCallback?()
            case .failure(message: let message):
                self.view.hideToastActivity()
                self.view.makeToast(message, position: .center)
            }
        })
        .disposed(by: disposeBag)

        // 首次进入也要启动计时器
        sendSMS.onNext(false)
    }

    @objc
    func timerButtonAction() {
        timerButton.isEnabled = false
        timerButton.setTitle("\(60)\(LoginModular.localizedString(key: "Verify.Label.Timer"))", for: .disabled)
        sendSMS.onNext(true)
    }

    func setupTimerButton(remain: Int) {
        if remain > 0 {
            timerButton.isEnabled = false
            timerButton.setTitle("\(remain)\(LoginModular.localizedString(key: "Verify.Label.Timer"))", for: .disabled)
        } else {
            timerButton.isEnabled = true
        }
    }

    func setupCodeUI(code: String) {
        codeTextField.text = code
        code1Label.text = code.slicing(from: 0, length: 1)
        code2Label.text = code.slicing(from: 1, length: 1)
        code3Label.text = code.slicing(from: 2, length: 1)
        code4Label.text = code.slicing(from: 3, length: 1)
        switch code.count {
        case 0:
            selectLine(line1)
        case 1:
            selectLine(line2)
        case 2:
            selectLine(line3)
        case 3:
            selectLine(line4)
        default:
            selectLine(nil)
        }
    }

    func selectLine(_ line: UIView?) {
        line1.backgroundColor = Adaptor.Login.Verify.normalLineColor
        line2.backgroundColor = Adaptor.Login.Verify.normalLineColor
        line3.backgroundColor = Adaptor.Login.Verify.normalLineColor
        line4.backgroundColor = Adaptor.Login.Verify.normalLineColor
        line?.backgroundColor = Adaptor.Login.Verify.selectedLineColor
    }
}
